// map.js
import qqMapSdk from '../../../utils/qqMapSdk'

Page({
  data: {
    latitude: 40.060539,
    longitude: 116.343847,
    scale: 16,
    markers: [
      {
        id: 1,
        latitude: 40.22077,
        longitude: 116.23128,
        width: 24,
        height: 30
      },
      {
        id: 2,
        latitude: 40.225857999999995,
        longitude: 116.23246699999999,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      }
    ],
    polyline: [],
    detailInfo: {}
  },
  onLoad (query) {
    this.query = query
    console.log(this.query)
    // 获取详情
    this.getDetail()
    // 生成路线
    this.getPolyline()
  },
  // 获取路径
  getPolyline () {
    qqMapSdk.direction({
      mode: 'bicycling',
      from: {
        latitude: 40.22077,
        longitude: 116.23128
      },
      to: {
        latitude: 40.225857999999995,
        longitude: 116.23246699999999
      },
      success: ({ result }) => {
        const coors = result.routes[0].polyline
        const points = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000
        }
        // 获取经纬度
        for (let i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        // 渲染数据
        this.setData({
          latitude: points[30].latitude,
          longitude: points[30].longitude,
          polyline: [{ points, color: '#5591af', width: 4 }]
        })
      }
    })
  },
  // 获取详情
  async getDetail () {
    const { data } = await wx.http.get(`/repair/${this.query.id}`)
    console.log(data)
    this.setData({
      detailInfo: data
    })
  },
  // 取消保修信息
  async cancelRepair (e) {
    await wx.http.put(`/cancel/repaire/${e.mark.id}`)
    wx.utils.toast('取消报修成功')
    wx.navigateBack({ delta: 1 })
  },
  // 修改保修信息
  editRepair (e) {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + e.mark.id
    })
  }
})
